'use strict';

const Controller = require('egg').Controller;

class ArticalController extends Controller {
    //发表文章
    async add() {
        //title con type username
        let {ctx,service} = this;
        // let errs = app.validator.validate({ userName: 'string' }, ctx.request.body);
        
        try{
            ctx.validate({ title: 'string',con:'string',type:'string',username:'string' })
            //执行添加
            try{
                let {title,con,type,username} = ctx.request.body;
                await service.artical.add(title,con,type,username);
                ctx.body = {
                    code:1,
                    msg:'添加成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }

        }catch(e){
            ctx.body = {
                code:2,
                msg:e
            }
        }
    }
    async update(){
        //title con type username
        let {ctx,service} = this;
        // let errs = app.validator.validate({ userName: 'string' }, ctx.request.body);
        
        try{
            ctx.validate({ title: 'string',con:'string',type:'string',id:'string' })
            //执行添加
            try{
                let {title,con,type,id} = ctx.request.body;
                await service.artical.update(title,con,type,id);
                ctx.body = {
                    code:1,
                    msg:'修改成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }

        }catch(e){
            ctx.body = {
                code:2,
                msg:e
            }
        }
    }
    async del(){
        let {ctx,service} = this;
        let {id} = ctx.request.query;
        if(id){
            try{
                await service.artical.del(id);
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
    //查询
    async articalList(){
        let {ctx,service} = this;
        //分页 pagenum limit  搜索 key
        let {pagenum=1,limit=2,key=''} = ctx.request.query;
        //起始的下标
        let startIndex = (pagenum-1)*limit;
        
        let list = await service.artical.articalList(startIndex,limit,key);
        let total = await service.artical.total(key);
        console.log(total);
        ctx.body = {
            code:1,
            data:list,
            total:total[0]['count(*)']
        }
    }
    //类目查询
    async articalTypeList(){
        let {ctx,service} = this;
        let {type=''} = ctx.query;
        let list = await service.artical.articalTypeList(type);
        ctx.body = {
            code:1,
            data:list
        }
    }

    //按作者查询
    async articalUsername(){
        let {ctx,service} = this;
        let {username=''} = ctx.query;
        let list = await service.artical.articalUsername(username);
        ctx.body = {
            code:1,
            data:list
        }
    }
}

module.exports = ArticalController;
